package com.hkyl.hospital.controller;

import com.github.pagehelper.PageInfo;
import com.hkyl.hospital.domain.ResultVo;
import com.hkyl.hospital.entity.Medicine;
import com.hkyl.hospital.service.MedicineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author gao
 * @date 2023/07/10 16:52:34
 */
@RestController
@RequestMapping("medicine")
public class MedicineController {
    @Autowired
    private MedicineService medicineService;

    @PostMapping("findSomeByPage/{pageNum}/{pageSize}")
    public ResultVo findSomeByPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        List<Medicine> list = medicineService.findSomeByPage(pageNum,pageSize);
        PageInfo<Medicine> pageInfo=new PageInfo<>(list);
        return  ResultVo.success(pageInfo);
    }
    @PostMapping("save")
    public ResultVo save(@RequestBody Medicine medicine){
        int rows = medicineService.save(medicine);
        return rows>0?ResultVo.success("增加成功"):ResultVo.failure("增加失败");
    }
    @PostMapping("update")
      public ResultVo update(@RequestBody Medicine medicine){
        int rows = medicineService.update(medicine);
        return  rows>0?ResultVo.success("修改成功"):ResultVo.failure("修改失败");
    }
    @PostMapping("delete")
    public ResultVo delete(@RequestBody String medicineId){
        if (!StringUtils.hasText(medicineId)) {
            throw new RuntimeException("不存在输入的id");
        }
        int rows = medicineService.delete(Integer.valueOf(medicineId));
        return  rows>0?ResultVo.success("删除成功"):ResultVo.failure("删除失败");
    }
    @PostMapping("findOne")
        public ResultVo findOne(@RequestBody String medicineId){
            if (!StringUtils.hasText(medicineId)) {
                throw new RuntimeException("不存在输入的id");
            }
            Medicine medicine = medicineService.findOne(Integer.valueOf(medicineId));
            return  ResultVo.success(medicine);
        }
    @PostMapping("changStatus")
    public  ResultVo changStatus(@RequestBody String medicineId){
        if (!StringUtils.hasText(medicineId)) {
            throw new RuntimeException("不存在输入的id");
        }
        int i = medicineService.changeStatus(Integer.valueOf(medicineId));
        return  ResultVo.success("success");
    }

    @PostMapping("list")
    public ResultVo findAll(){
        return ResultVo.success(medicineService.findAll());
    }

    @PostMapping("useMedicineType")
    public ResultVo getUseMedicineType(){
        List<String> useMedicineType = medicineService.getUseMedicineType();
        return ResultVo.success(useMedicineType);
    }
}
